﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;

namespace MonoFrameWork
{
    public class Sprite : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public Texture2D texture;
        public Vector2 position, velocity;
        public SpriteBatch sp;
        public bool showBorder = false;
        public Game game;
        //Texture để thực hiện load
        // Khởi tạo Sprite
        public Sprite(Game game)
            : base(game)
        {
            sp = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            this.game = game;
            Game.Components.Add(this);
        }
        public Sprite(Game game, Vector2 pos, Vector2 velo)
            : base(game)
        {
            this.position = pos;
            this.velocity = velo;
            sp = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            this.game = game;
            Game.Components.Add(this);
        }
        public Sprite(Game game, Texture2D texture, Vector2 pos, Vector2 velo)
            : base(game)
        {
            this.texture = texture;
            this.position = pos;
            this.velocity = velo;
            this.game = game;
            sp = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            Game.Components.Add(this);
        }
        //Vùng bao phủ của texture
        public Rectangle getBound()
        {
            return new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
        }
        // Hàm Update cập nhật lại vị trí của Sprite sau khi thực hiện gia tốc
        public override void Update(GameTime gameTime)
        {
            position += velocity;

            base.Update(gameTime);
        }
        //thực hiện vẽ Texture lên game
        public override void Draw(GameTime gameTime)
        {
            sp.Begin();

            sp.Draw(texture, position, Color.White);

            //Kiểm tra điều kiện cho phép vẽ viền cho sprite hay không
            if (showBorder)
            {
                sp.Draw(CreateRectangle(texture.Width, texture.Height, Color.Transparent), position, Color.White);
            }
            sp.End();

            base.Draw(gameTime);
        }
        //Tạo khung hình bao quanh đối tượng sprite
        private Texture2D CreateRectangle(int width, int height, Color colori)
        {
            Texture2D rectangleTexture = new Texture2D(game.GraphicsDevice, width, height, false, SurfaceFormat.Color);// create the rectangle texture, ,but it will have no color! lets fix that
            Color[] color = new Color[width * height];//set the color to the amount of pixels in the textures
            for (int i = 0; i < color.Length; i++)//loop through all the colors setting them to whatever values we want
            {
                if (i < width)
                    color[i] = Color.Red;
                else
                    if ((i % width) <= 0 || (i % width) == width - 1)
                        color[i] = Color.Red;
                    else
                        if (i / width == height - 1)
                            color[i] = Color.Red;
                        else
                            color[i] = colori;
            }
            rectangleTexture.SetData(color);//set the color data on the texture
            return rectangleTexture;//return the texture
        }
        public bool CheckCollides(Sprite sprite)
        {
            return this.getBound().Intersects(sprite.getBound());
        }
        public bool CheckCollides(SpriteAnimation sprite)
        {
            return this.getBound().Intersects(sprite.getBound());
        }
        public bool CheckCollides(Animation animation)
        {
            return this.getBound().Intersects(animation.getBound());
        }
        public bool CheckCollides(Texture2D texture, Vector2 position)
        {
            Rectangle rect = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
            return this.getBound().Intersects(rect);
        }
    }
}
